package com.shiningstars.service;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;

import com.parse.ParsePush;
import com.shiningstars.activity.MyFeedbackActivity;
import com.shiningstars.activity.MyFeedbackActivity.HandlerText;
import com.shiningstars.database.DatabaseHandler;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Utilities;
import com.shiningstars.php.WebHelper;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Feedback;

public class FeedbackNotifyService extends Service implements Constants, Config {

	int attempts = SERVICE_ATTEMPTS;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

		new Thread() {
			@Override
			public void run() {
				try {
					DatabaseHandler databaseHandler = DatabaseHandler.getInstance(getApplicationContext());

					while (attempts > 0) {
						attempts--;
						while (true) {
							Feedback feedback = databaseHandler.getFeedbackToNotify();

							if (null != feedback) {
								JSONObject pushJSON = new JSONObject();
								pushJSON.put(ACTION, START_SERVICE);
								pushJSON.put(SERVICE_NAME, ALL);

								ParsePush push = new ParsePush();
								push.setChannel(ADMIN);
								push.setData(pushJSON);
								push.sendInBackground();

								databaseHandler.updateFeedbackIsNotified(feedback.getId(), feedback.getIsNotified());
							} else {
								break;
							}
						}

						while (true) {
							Feedback feedback = databaseHandler.getFeedbackToSyncNotify();

							if (null != feedback) {
								try {
									WebHelper webHelper = new WebHelper();
									JSONObject json = webHelper.notifyFeedback(feedback, getApplicationContext());
									if (null != json && json.getBoolean(SUCCESS)) {
										databaseHandler.updateFeedbackIsNotified(feedback.getId(),
												feedback.getIsNotified());

										HandlerText handlerText = MyFeedbackActivity.handlerText;
										if (null != handlerText) {
											Message message = handlerText.obtainMessage();
											message.obj = feedback;
											MyFeedbackActivity.handlerText.sendMessage(message);
										}
									}
								} catch (JSONException e) {
									Utilities.logError(getApplicationContext(), FeedbackNotifyService.class.getName(),
											e);
									break;
								} catch (IllegalStateException e) {
									Utilities.logError(getApplicationContext(), FeedbackNotifyService.class.getName(),
											e);
									break;
								} catch (IOException e) {
									break;
								}
							} else {
								break;
							}
						}

						Thread.sleep(SERVICE_THREAD_SLEEP);
					}

					stopService(new Intent(getApplicationContext(), FeedbackNotifyService.class));

				} catch (Exception e) {
					Utilities.logError(getApplicationContext(), FeedbackNotifyService.class.getName(), e);
				}
			}
		}.start();

		return START_NOT_STICKY;
	}
}